package com.controller;


import com.dao.FAddressDao;
import com.model.FAddress;
import com.model.FUser;
import com.model.Page;
import com.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AddressController {
    @Resource
    FAddressDao fAddressDao;
    @Resource
    Result result;

    private Result setResult(int status, String msg) {
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    //获取当前用户的收货地址
    @GetMapping(value = "/myAddress")
    public String getMyAddress(ModelMap model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("USER_SESSION");
        FAddress fAddress = fAddressDao.getAddressByUserId(user.getId());
        model.addAttribute("fAddress", fAddress);
        model.addAttribute("user", user);
        return "front/user_center_site";
    }

    //后台收货地址显示列表
    @GetMapping(value = "/addressList")
    public String List(ModelMap model, Page page) {
        List<FAddress> list = fAddressDao.list(page);
        int total = fAddressDao.total(page);
        page.caculateLast(total);
        model.addAttribute("list", list);
        model.addAttribute("page", page);
        model.addAttribute("total", total);
        return "backend/address-list";
    }
    //添加收货地址
    @PostMapping(value = "/addAddress")
    public String addAddress(FAddress fAddress, HttpServletRequest request){
        try{
            HttpSession session=request.getSession();
            FUser user=(FUser) session.getAttribute("USER_SESSION");
            FAddress fAddress1=fAddressDao.getAddressByUserId(user.getId());
            fAddress.setUser_id(user.getId());
            if (fAddress1!=null){
                fAddress.setId(fAddress1.getId());
                fAddressDao.updateAddress(fAddress);
            }else {
                fAddressDao.insertAddress(fAddress);
            }
            return "redirect:myAddress";
        }catch (Exception ex){
            return "front/login";
        }
    }

    //删除收货地址
    @PostMapping(value = "/delAddress")
    @ResponseBody
    public Result delAddress(Integer id, ModelMap model){
        try{
            FAddress fAddress= fAddressDao.getAddressById(id);
            if(fAddress==null){
                return this.setResult(0,"地址不存在！");
            }else {
                    int row=fAddressDao.deleteAddress(id);
                    if(row>0){
                        return this.setResult(1,"删除类型成功！");
                    }else {
                        return this.setResult(0,"删除管分类出错，请联系管理员！");
                    }

            }
        }catch (Exception ex){
            return this.setResult(0,"删除分类出错，请联系管理员！");
        }
    }

    //修改收货地址
    @PostMapping(value = "/updateAddress")
    @ResponseBody
    public Result updateAddress(FAddress fAddress, ModelMap model){
        try {
            int row =fAddressDao.updateAddress(fAddress);
            if(row>0){
                return  this.setResult(1,"修改收货地址成功！");
            }else {
                return  this.setResult(0,"修改收货地址出错，请联系管理员！");
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }
}